package com.ews.onlineexchange.service.schedule;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;

import javax.annotation.Resource;
import javax.ejb.Schedule;
import javax.ejb.ScheduleExpression;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.ejb.TimerConfig;
import javax.ejb.TimerService;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.inject.Inject;
import javax.persistence.EntityManager;

import org.hibernate.CacheMode;
import org.hibernate.FlushMode;
import org.hibernate.Query;
import org.hibernate.ScrollMode;
import org.hibernate.ScrollableResults;
import org.hibernate.Session;

import com.ews.onlineexchange.model.QualificationCert;
import com.ews.onlineexchange.model.RejectRecord;

@Singleton
@Startup
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public class OracleSessionCountTimer {

	@Resource
	TimerService timerService;
	
	@Inject
	EntityManager entityManager;
	
//	@PostConstruct
	public void init() {
		ScheduleExpression schedule = new ScheduleExpression();	
		schedule.hour(2);
		timerService.createCalendarTimer(schedule, new TimerConfig("oracleSessionCountTimer", false));
		
//		check();
	}
	
	private static final String QUERY_SESSION_COUNT = ""
			+ "	SELECT			count(*)"
			+ "	FROM			V$PROCESS  ";
//	@Timeout
	@Schedule(hour="*/2", minute="0", second="0", info = "oracleSessionCountTimer", persistent = false)
	public void check() {
		System.out.println("当前时间:"+new Date());
		BigDecimal count =  (BigDecimal)entityManager.createNativeQuery(QUERY_SESSION_COUNT).getSingleResult();
		System.out.println("当前数据库连接数:"+count);
	}
}
